<?php

namespace app\admin\controller;

use think\Controller;
use think\File;
use think\Request;
use think\Seeeion;
use think\Db;
use org\Qiniu;
use org\Upayun;

class Upload extends Base
{

    /**
     * [upload上传图片到七牛云]
     * @throws \Exception
     */
    public function upload(){
        $filePath = $_FILES['file']['tmp_name'];
        //取出图片后缀
        $type = explode(".",$_FILES['file']['name']);
        $type = end($type);
        //组装图片名
        $key = md5(time().uuid()).'.'.$type;
        $up = new Qiniu();
        $data = $up->uploadFile($filePath,$key);
        echo $data;
    }

    /**
     * [deleteImg 删除七牛图片文件]
     * @return \think\response\Json
     */
    public function deleteImg(){
        $add = input('add');
        $up = new Qiniu();
        $res = $up->delFile($add,'kevin');
        if($res){
            return json(['code'=>100,'msg'=>'删除失败！']);
        }else{
            return json(['code'=>200,'msg'=>'删除成功！']);
        }
    }

    /**
     * [layUpload layui上传图片&音频]
     * @return \think\response\Json
     * @throws \Exception
     */
    public function layUpload(){
        if (config('qiniu_allow_state') == 0) {
            set_time_limit (0);
            $filePath = $_FILES['file']['tmp_name'];
            //取出图片后缀
            $type = explode(".",$_FILES['file']['name']);
            $type = end($type);
            //组装图片名
            $key = md5(time().uuid()).'.'.$type;
            $up = new Qiniu();
            $data = $up->uploadFile($filePath,$key);
            return json(['code'=>0,'msg'=>'','data'=>['src'=>config('domain').'/'.$data]]);
        } else {
            return json(['code'=>1,'msg'=>'抱歉，请联系后台管理员开启线上上传功能','data'=>'']);
        }
    }

    /**
     * [uploadLocality 图片上传至本地&压缩]
     * @return mixed
     */
    public function uploadLocality(){
        $file = request()->file('file');
        $dir = ROOT_PATH . 'public' . DS . 'uploads/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
//        $url = http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            //压缩图片
//            image_png_size_add(ROOT_PATH . 'public' . DS . 'uploads/images/'.$newName,ROOT_PATH . 'public' . DS . 'uploads/images/'.$newName);
            echo "/uploads/images/{$newName}";
        }else{
            echo $file->getError();
        }
    }

    /**
     * [deleteLocality 删除本地图片]
     * @return \think\response\Json
     */
    public function deleteLocality(){
        $add  = input('add');
        $add = substr ($add,1);
        if(unlink($add)){
            return json(['code'=>200,'msg'=>'删除成功！']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败！']);
        }
    }

    /**
     * [updateFace 后台用户修改头像上传]
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateFace(){
        $base64url = input('base64url');
        $arr = base64_img($base64url,true);
        if($arr['code'] == 200){
            $res = Db::name('admin')->where('id',input('id'))->update(["portrait"=>$arr['msg']]);
            if($res){
                session('portrait', $arr['msg']); //用户头像
                return json(['code'=>200,'msg'=>"上传成功"]);
            }else{
                return json(['code'=>100,'msg'=>"上传失败"]);
            }
        }elseif($arr['code'] == 100){
            writelog('管理员上传头像失败',100);
            return json($arr);
        }
    }

    /**
     * [webuploader 多图上传测试页面]
     * @return mixed
     */
    public function webuploader(){
        return $this->fetch('/webuploader');
    }

    /**
     * [showImg 多图修改测试页面]
     * @return mixed
     */
    public function showImg(){
        $photo = Db::name('img')->where('id',1)->value('img');
        $arr = explode(',',$photo);
        $this->assign ('photo',$photo);
        $this->assign ('arr',$arr);
        return $this->fetch('/webuploader2');
    }
}